var ctx

Page({

  data: {  canvasWidth:'90%',canvasHeight:'80%',color:'255,0,0',url1:'',type:'',near_id:'',goods_id:'',radio:'1',url:'',last:'',beautifulfile:'',
  //生成的底色临时url
  url2:'',data1:'',baseurl:'',backurl:'',file:'',index:0,isindex:0,index2:0,pagex:0,beautiful:[0,0,0,0],file1:'',chuncolor:[0,0,0,0],show:false,
  windowwidth:'',selectindex:1

  },
 
//  onChange(e){
//    console.log(e);
//    if(this.data.radio==e.detail){return}
//    let color
//      this.setData({
//        radio:e.detail
//      })
//      if(e.detail==1){
//         color='255,255,255'
//      }else if(e.detail==2){
//          color='255,0,0'
//      }else if(e.detail==3){
//        color='0,162,255'

//      }
//  this.getdise(color)


//  },
 onLoad(options){
   wx.getSystemInfo({
     complete: (res) => {
       console.log(res);
       this.setData({
         windowwidth:res.windowWidth
       })
       
     },
   })
     this.setData({
       url:options.url,
       baseurl:options.baseurl,
       goods_id:options.goods_id,
       near_id:options.near_id,
       type:options.type,
       file:options.file,
       file1:'https://zujieba.com/uploads/'+options.file
     })
 },
 getdise(color,file,flag){
   console.log(flag,file);
   
   let file1=this.data.file
   if(file){

     file1=file
     console.log(file1);
     
   }
   console.log(1111);
   
   
   wx.request({
     url: 'https://zujieba.com/Api/upload/baidu_color.html',
     data:{file:file1,color},
     method:'POST',
     success:(res)=>{
      let url="https://zujieba.com/uploads/"+res.data.data
       console.log(res);
       wx.hideLoading()
       this.setData({
         show:false,
         url,
         baseurl:res.data.data
       })
       wx.hideLoading()
       
       
     }
   })
 },
 edit(){
   console.log(this.data.type);
   
    wx.navigateTo({
      url: '/pages/edit5/edit5?url='+this.data.url+"&flag=16"+"&type="+this.data.type,
    })
 },
 sureclick(){
   this.throttle(()=>{
    console.log('dianjile',this.data.baseurl);
     
    if(this.data.backurl){
     this.getshangchuan(this.data.backurl)
    }else{this.getprint(this.data.baseurl,this.data.type)}
   
   },1000)
   
   
 },
 getshangchuan(path){
   console.log(11);
   
  
      wx.uploadFile({
        filePath: path,
        name: 'file',
        url: 'https://zujieba.com/Api/upload/upload.html',
        success:(res)=>{
          console.log(res);
           let url=JSON.parse(res.data).name
           this.getprint(url,this.data.type)
          
        },
        fail:(res)=>{
          console.log(res);
          
        }
      })
 },
 getprint(file,type){
  wx.showLoading({
    title: '正在提交...',
  })
   wx.request({
     url: 'https://zujieba.com/Api/upload/photo11.html',
     data:{pic:file,type:type},
     method:'POST',
     success:(res)=>{
       wx.hideLoading()
       console.log(res);
       if(res.data.success==1){
        let url="https://zujieba.com/uploads/"+res.data.data
        
         
         wx.navigateTo({
           url: '/pages/fileprint/print/newprint?url='+url+'&near_id='+this.data.near_id+'&goods_id='+this.data.goods_id+"&time="+res.data.time+"&name="+res.data.data+"&photo=1"+'&flag=17'+"&radio5=0"+"&index2=0"+'&printtype='+this.data.type
         })
        
       }else{
         wx.showToast({
           title: res.data.tips,
           icon:'none'
         })
       }
       
     }
   })
 },
 throttle:function(func,wait){
  
  
  let last =this.data.last
  
  
  let current_time= +new Date()
  if(current_time-last>wait){
    
      
      
      func()
      last=+new Date()
      this.setData({
        last
      })
  }
    
     
  
   
},
togglecolor(e){

 
  
  if(this.data.index==e.currentTarget.dataset.index){return}
  this.setData({
    chuncolor:[0,0,0,0],
    show:true
  })
  console.log(e);
  wx.showLoading({
    title: '生成中',
  })
 
   let color
    
     if(e.currentTarget.dataset.index==0){
        color='255,0,0'
     }else if(e.currentTarget.dataset.index==1){
         color='255,255,255'
     }else if(e.currentTarget.dataset.index==2){
       color='0,162,255'

     }
     this.setData({index:e.currentTarget.dataset.index,color})
     let whitening=parseInt(this.data.beautiful[0]/261*100)
  let smoothing=parseInt(this.data.beautiful[1]/261*100)
  let facelifting=parseInt(this.data.beautiful[2]/261*100)
  let eyeenlarging=parseInt(this.data.beautiful[3]/261*100)
     this.getbeautphoto(whitening,smoothing,facelifting,eyeenlarging)
 

  
   
},
toggleindex(e){
  
  
  this.setData({isindex:e.currentTarget.dataset.isindex})
},
getwidth(e){
  console.log(e);
  if(e.touches[0].pageX>306||e.touches[0].pageX<45){
    return
  }
  let i=this.data.index2-1
  let arr=this.data.beautiful
  arr.forEach((item,index)=>{
    if(index==i){
      console.log(i,111);
      
    arr[index]=e.touches[0].pageX-45
     console.log(arr);
     
    this.setData({
      beautiful:arr
    })
    }
  })
  this.throttle(()=>{
    this.getbeautphoto()
  },1000)
 
 
  
},
movewidth(e){
  console.log(e);
  let windowwidth=this.data.windowwidth-25
  let pagex=e.changedTouches[0].pageX
  let offsetLeft=pagex-50
  if(pagex>windowwidth*0.8+50){
    offsetLeft=windowwidth*0.8
  }else if(pagex<50){
    offsetLeft=0
  }
  console.log(offsetLeft);
  
  if(this.data.index2==6){
    let arr=this.data.chuncolor
    arr[0]=parseInt((offsetLeft)/windowwidth*0.8*100)
    this.setData({chuncolor:arr})
    console.log(arr);
    
    return
  }
  
  let i=this.data.index2-1
  let arr=this.data.beautiful
  arr.forEach((item,index)=>{
    if(index==i){
     
      
    arr[index]=offsetLeft
     
     
    this.setData({
      beautiful:arr
    })
    }
  })
 

},
movewidth1(e){
  
  if(e.touches[0].pageX>293||e.touches[0].pageX<45){
    return
  }
  
    let arr=this.data.chuncolor
    arr[3]=e.touches[0].pageX-45
    this.setData({chuncolor:arr})
    console.log(arr);
    
    
 
  
  
 

},
movewidth2(e){
  
  if(e.touches[0].pageX>293||e.touches[0].pageX<45){
    return
  }
  
    let arr=this.data.chuncolor
    arr[0]=e.touches[0].pageX-45
    this.setData({chuncolor:arr})
    console.log(arr);
    
    
 
  
  
 

},
endwidth(e){
  let windowwidth=this.data.windowwidth-25
  let pagex=e.changedTouches[0].pageX
  let offsetLeft=pagex-50
  if(pagex>windowwidth*0.8+50){
    offsetLeft=windowwidth*0.8
  }else if(pagex<50){
    offsetLeft=0
  }
  
  
  this.setData({show:true})
  wx.showLoading({
    title: '生成中',
    
  })
  
  let i=this.data.index2-1
  let arr=this.data.beautiful
  arr.forEach((item,index)=>{
    if(index==i){
     
      
    arr[index]=offsetLeft
     
     
    this.setData({
      beautiful:arr
    })
    }
  })
  let whitening=parseInt(arr[0]/windowwidth*100)
  let smoothing=parseInt(arr[1]/windowwidth*100)
  let facelifting=parseInt(arr[2]/windowwidth*100)
  let eyeenlarging=parseInt(arr[3]/windowwidth*100)
  this.throttle(()=>{
    this.getbeautphoto(whitening,smoothing,facelifting,eyeenlarging)
  },1000)
},
endwidth1(e){
  this.setData({show:true})
  console.log(e);
  wx.showLoading({
    title: '生成中',
    
  })
  if(e.currentTarget.offsetLeft>261||e.currentTarget.offsetLeft<0){
    return
  }
  let a=parseInt(e.currentTarget.offsetLeft/261*100)
  let r=parseInt(this.data.chuncolor[0])
  if(r>255){
    r=255
  }
  let file='https://zujieba.com/uploads/'+this.data.file
  if(this.data.beautifulfile!=''){
    console.log('不为空');
    
    file=this.data.beautifulfile
  }
  this.getchuncolor(r,a,file)
  
   
},
endwidth2(e){
  this.setData({show:true})
  console.log(e);
  wx.showLoading({
    title: '生成中',
    
  })
  if(e.currentTarget.offsetLeft>261||e.currentTarget.offsetLeft<0){
    return
  }
  let a=parseInt(this.data.chuncolor[3]/261*100)
  let r=parseInt(e.currentTarget.offsetLeft)
  if(r>255){
    r=255
  }
  let file='https://zujieba.com/uploads/'+this.data.file
  if(this.data.beautifulfile!=''){
    console.log('不为空');
    
    file=this.data.beautifulfile
  }
  this.getchuncolor(r,a,file)
  
   
},
getchuncolor(r,g,b,a,file){
  console.log(file);
  
   wx.request({
     url: 'https://zujieba.com/Api/photo/show',
     data:{r,g,b,a,url:file},
     method:'POST',
     success:(res=>{
     



       console.log(res);
        //传到下载地址
        wx.request({
          url: 'https://zujieba.com/Api/photo/download',
          data:{url:res.data.ResultUrl},
          method:'POST',
          success:(res1)=>{
            console.log(res1);
            wx.request({
              url: 'https://zujieba.com/Api/upload/baidu_koutu.html',
              data:{file:res1.data.data},
              method:'POST',
              success:(res2)=>{
                this.setData({file1:'https://zujieba.com/uploads/'+res2.data.data})
                this.getdise(this.data.color,res2.data.data)
              }})
            
            
            
          }
        })
       
     })
   })
},
togglestyle(e){
   this.setData({
     index2:e.currentTarget.dataset.index
   })
   if(e.currentTarget.dataset.index==5){
    let whitening=50
    let smoothing=10
    let facelifting=70
    let eyeenlarging=70
    this.setData({show:true})
    wx.showLoading({
      title: '生成中',
    })
     this.getbeautphoto(whitening,smoothing,facelifting,eyeenlarging)
   } 
   if(e.currentTarget.dataset.index==0){
    let whitening=0
    let smoothing=0
    let facelifting=0
    let eyeenlarging=0
    this.setData({show:true})
    wx.showLoading({
      title: '生成中',
    })
     this.getbeautphoto(whitening,smoothing,facelifting,eyeenlarging)
   }
},
toggleshow(){
   this.setData({
     index2:0
   })
},
getbeautphoto(whitening,smoothing,facelifting,eyeenlarging){
     console.log('jici');
     let file=this.data.file1
     if(this.data.index2==0||this.data.index2==5){
       file='https://zujieba.com/uploads/'+this.data.file
     }
     if(this.data.selectindex==0){
      file='https://zujieba.com/uploads/'+this.data.file
     }
  let params={whitening,smoothing,facelifting,eyeenlarging,url:file}
  console.log(params);
  
   wx.request({
     url: 'https://zujieba.com/Api/photo/index',
     data:{whitening,smoothing,facelifting,eyeenlarging,url:file},
     method:'POST',
     success:(res)=>{
       console.log(res);
       
       //传到下载地址
       wx.request({
         url: 'https://zujieba.com/Api/photo/download',
         data:{url:res.data.ResultUrl},
         method:'POST',
         success:(res1)=>{
           console.log(res1);
           this.setData({beautifulfile:'https://zujieba.com/uploads/'+res1.data.data})
           
           this.getdise(this.data.color,res1.data.data)
         }
       })
       
     }
   })
},
selcetclick(e){
  wx.showLoading({
    title: '生成中',
  })
  console.log(e);
  let index=e.currentTarget.dataset.index
  let file='https://zujieba.com/uploads/'+this.data.file
  console.log(file);
  
  if(this.data.beautifulfile!=''){
    file=this.data.beautifulfile
    console.log(file);
    
  }
  this.setData({
    selectindex:index,
    show:true
  })
  let r=''
  let g=''
  let b=''
  let a=''
  if(index==0){
  file='https://zujieba.com/uploads/'+this.data.file
  console.log(file);
  let whitening=parseInt(this.data.beautiful[0]/this.data.windowwidth*100*0.8)
  let smoothing=parseInt(this.data.beautiful[1]/this.data.windowwidth*100*0.8)
  let facelifting=parseInt(this.data.beautiful[2]/this.data.windowwidth*100*0.8)
  let eyeenlarging=parseInt(this.data.beautiful[3]/this.data.windowwidth*100*0.8)
  this.throttle(()=>{
    this.getbeautphoto(whitening,smoothing,facelifting,eyeenlarging)
  },1000)
  return
  }
  if(index==1){
    r=255
    g=120
    b=120
    a=50
  }
  if(index==2){
    r=255
    g=86
    b=86
    a=50
  }
  if(index==3){
    r=255
    g=45
    b=45
    a=50
  }
  if(index==4){
    r=255
    g=163
    b=184
    a=50
  }
  if(index==5){
    r=255
    g=166
    b=166
    a=50
  }
  if(index==6){
    r=245
    g=50
    b=120
    a=50
  }
  if(index==7){
    r=251
    g=72
    b=93
    a=50
  }
  if(index==8){
    r=164
    g=0
    b=1
    a=50
  }
  console.log(r,g,b,a);
  
   this.getchuncolor(r,g,b,a,file)
}

})